﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using NCT.Framework;
using VISS.API.Commons;

namespace VISS.Web.Frontend {

    /// <summary>
    /// Dùng để build Select
    /// </summary>
    public class SelectCtrlBuilder {
        private const string _template = "<option value=\"{0}\" {1}>{2}</option>";

        /// <summary>
        /// Select City
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlCities(short value) {
            ICity city = StructureMapFactory.GetInstance<ICity>();
            List<CityEnt> listCities = city.GetAll();
            string str = "";
            for (int i = 0; i < listCities.Count(); i++) {
                string selected = "";
                if (listCities[i].ID == value) {
                    selected = "selected=\"selected\"";
                }
                else {
                    selected = "";
                }
                str += string.Format(_template, listCities[i].ID, selected, listCities[i].Name);
            }

            return str;
        }

        /// <summary>
        /// Select City For Search
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlCitiesForSearch(short value) {
            ICity city = StructureMapFactory.GetInstance<ICity>();
            List<CityEnt> listCities = city.GetAll();
            string str = "";
            string selected = "";

            if (value == -1) {
                selected = "selected=\"selected\"";
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                selected = "";
                for (int i = 0; i < listCities.Count(); i++) {
                    str += string.Format(_template, listCities[i].ID, selected, listCities[i].Name);
                }
            }
            else {
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                for (int i = 0; i < listCities.Count(); i++) {
                    if (listCities[i].ID == value) {
                        selected = "selected=\"selected\"";
                    }
                    else {
                        selected = "";
                    }
                    str += string.Format(_template, listCities[i].ID, selected, listCities[i].Name);
                }
            }
            return str;
        }

        /// <summary>
        /// Select job
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlJob(short value) {
            IJob job = StructureMapFactory.GetInstance<IJob>();
            List<JobEnt> listjob = job.GetAll();
            string str = "";
            for (int i = 0; i < listjob.Count(); i++) {
                string selected = "";
                if (listjob[i].ID == value) {
                    selected = "selected=\"selected\"";
                }
                else {
                    selected = "";
                }
                str += string.Format(_template, listjob[i].ID, selected, listjob[i].Name);
            }

            return str;
        }

        /// <summary>
        /// Select Job For Search
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlJobForSearch(short value) {
            IJob job = StructureMapFactory.GetInstance<IJob>();
            List<JobEnt> listjob = job.GetAll();
            string str = "";
            string selected = "";

            if (value == -1) {
                selected = "selected=\"selected\"";
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                selected = "";
                for (int i = 0; i < listjob.Count(); i++) {
                    str += string.Format(_template, listjob[i].ID, selected, listjob[i].Name);
                }
            }
            else {
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                for (int i = 0; i < listjob.Count(); i++) {
                    if (listjob[i].ID == value) {
                        selected = "selected=\"selected\"";
                    }
                    else {
                        selected = "";
                    }
                    str += string.Format(_template, listjob[i].ID, selected, listjob[i].Name);
                }
            }

            return str;
        }

        /// <summary>
        /// Select Religion - Tôn giáo
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlReligion(short value) {
            IReligion religion = StructureMapFactory.GetInstance<IReligion>();
            List<ReligionEnt> listReligion = religion.GetAll();
            string str = "";
            for (int i = 0; i < listReligion.Count(); i++) {
                string selected = "";
                if (listReligion[i].ID == value) {
                    selected = "selected=\"selected\"";
                }
                else {
                    selected = "";
                }
                str += string.Format(_template, listReligion[i].ID, selected, listReligion[i].Name);
            }

            return str;
        }

        /// <summary>
        /// Select Religion For Search - Tôn giáo
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlReligionForSearch(short value) {
            IReligion religion = StructureMapFactory.GetInstance<IReligion>();
            List<ReligionEnt> listReligion = religion.GetAll();
            string str = "";
            string selected = "";

            if (value == -1) {
                selected = "selected=\"selected\"";
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                selected = "";
                for (int i = 0; i < listReligion.Count(); i++) {
                    str += string.Format(_template, listReligion[i].ID, selected, listReligion[i].Name);
                }
            }
            else {
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                for (int i = 0; i < listReligion.Count(); i++) {
                    if (listReligion[i].ID == value) {
                        selected = "selected=\"selected\"";
                    }
                    else {
                        selected = "";
                    }
                    str += string.Format(_template, listReligion[i].ID, selected, listReligion[i].Name);
                }
            }

            return str;
        }

        /// <summary>
        /// Select Education - học vấn
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlEducation(short value) {
            IEducation education = StructureMapFactory.GetInstance<IEducation>();
            List<EducationEnt> listEducation = education.GetAll();
            string str = "";
            for (int i = 0; i < listEducation.Count(); i++) {
                string selected = "";
                if (listEducation[i].ID == value) {
                    selected = "selected=\"selected\"";
                }
                else {
                    selected = "";
                }
                str += string.Format(_template, listEducation[i].ID, selected, listEducation[i].Name);
            }

            return str;
        }

        /// <summary>
        /// Select Education For Search - học vấn
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlEducationForSearch(short value) {
            IEducation education = StructureMapFactory.GetInstance<IEducation>();
            List<EducationEnt> listEducation = education.GetAll();
            string str = "";
            string selected = "";

            if (value == -1) {
                selected = "selected=\"selected\"";
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                selected = "";
                for (int i = 0; i < listEducation.Count(); i++) {
                    str += string.Format(_template, listEducation[i].ID, selected, listEducation[i].Name);
                }
            }
            else {
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                for (int i = 0; i < listEducation.Count(); i++) {
                    if (listEducation[i].ID == value) {
                        selected = "selected=\"selected\"";
                    }
                    else {
                        selected = "";
                    }
                    str += string.Format(_template, listEducation[i].ID, selected, listEducation[i].Name);
                }
            }

            return str;
        }

        /// <summary>
        /// Select Marital Status - Tình trạng hôn nhân
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlMaritalStatus(short value) {
            IMaritalStatus earitalStatus = StructureMapFactory.GetInstance<IMaritalStatus>();
            List<MaritalStatusEnt> listMaritalStatus = earitalStatus.GetAll();
            string str = "";
            for (int i = 0; i < listMaritalStatus.Count(); i++) {
                string selected = "";
                if (listMaritalStatus[i].ID == value) {
                    selected = "selected=\"selected\"";
                }
                else {
                    selected = "";
                }
                str += string.Format(_template, listMaritalStatus[i].ID, selected, listMaritalStatus[i].Name);
            }

            return str;
        }

        /// <summary>
        /// Select Marital Status For Search - Tình trạng hôn nhân
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlMaritalStatusForSearch(short value) {
            IMaritalStatus earitalStatus = StructureMapFactory.GetInstance<IMaritalStatus>();
            List<MaritalStatusEnt> listMaritalStatus = earitalStatus.GetAll();
            string str = "";
            string selected = "";

            if (value == -1) {
                selected = "selected=\"selected\"";
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                selected = "";
                for (int i = 0; i < listMaritalStatus.Count(); i++) {
                    str += string.Format(_template, listMaritalStatus[i].ID, selected, listMaritalStatus[i].Name);
                }
            }
            else {
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                for (int i = 0; i < listMaritalStatus.Count(); i++) {
                    if (listMaritalStatus[i].ID == value) {
                        selected = "selected=\"selected\"";
                    }
                    else {
                        selected = "";
                    }
                    str += string.Format(_template, listMaritalStatus[i].ID, selected, listMaritalStatus[i].Name);
                }
            }

            return str;
        }

        /// <summary>
        /// Select Smoking - Hút thuốc
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlSmoking(short value) {
            ISmoking smoking = StructureMapFactory.GetInstance<ISmoking>();
            List<SmokingEnt> listSmoking = smoking.GetAll();
            string str = "";
            for (int i = 0; i < listSmoking.Count(); i++) {
                string selected = "";
                if (listSmoking[i].ID == value) {
                    selected = "selected=\"selected\"";
                }
                else {
                    selected = "";
                }
                str += string.Format(_template, listSmoking[i].ID, selected, listSmoking[i].Name);
            }

            return str;
        }

        /// <summary>
        /// Select Smoking For Search - Hút thuốc
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToSlSmokingForSearch(short value) {
            ISmoking smoking = StructureMapFactory.GetInstance<ISmoking>();
            List<SmokingEnt> listSmoking = smoking.GetAll();
            string str = "";
            string selected = "";

            if (value == -1) {
                selected = "selected=\"selected\"";
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                selected = "";
                for (int i = 0; i < listSmoking.Count(); i++) {
                    str += string.Format(_template, listSmoking[i].ID, selected, listSmoking[i].Name);
                }
            }
            else {
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                for (int i = 0; i < listSmoking.Count(); i++) {
                    if (listSmoking[i].ID == value) {
                        selected = "selected=\"selected\"";
                    }
                    else {
                        selected = "";
                    }
                    str += string.Format(_template, listSmoking[i].ID, selected, listSmoking[i].Name);
                }
            }

            return str;
        }


        /// <summary>
        /// Select Income - Thu nhập
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static readonly string[] InComeList = { 
                                        "0", "1000000", "2000000", "3000000",
                                        "5000000", "7000000", "10000000",
                                        "15000000", "20000000", "25000000",
                                        "30000000", "40000000", "50000000" };
        public static string ToSlIncome(int value) {

            string str = "";
            string selected = "";
            if (InComeList[0] == value.ToString()) {
                selected = "selected=\"selected\"";
            }
            str += string.Format(_template, InComeList[0], selected, "Không công bố");
            for (int i = 1; i < InComeList.Length; i++) {
                selected = "";
                if (InComeList[i] == value.ToString()) {
                    selected = "selected=\"selected\"";
                }
                else {
                    selected = "";
                }
                str += string.Format(_template, InComeList[i], selected, VISS.Web.Frontend.Commons.IntToViewString(ConvertUtils.ToInt(InComeList[i])));
            }

            return str;
        }

        public static string ToSlIncomeForSearch(int value) {

            string str = "";
            string selected = "";

            if (value == 0) {
                selected = "selected=\"selected\"";
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                selected = "";
                for (int i = 1; i < InComeList.Length; i++) {
                    str += string.Format(_template, InComeList[i], selected, VISS.Web.Frontend.Commons.IntToViewString(ConvertUtils.ToInt(InComeList[i])));
                }
            }
            else {
                str += string.Format(_template, "-1", selected, "--- Không quan tâm ---");
                for (int i = 1; i < InComeList.Length; i++) {
                    selected = "";
                    if (InComeList[i] == value.ToString()) {
                        selected = "selected=\"selected\"";
                    }
                    else {
                        selected = "";
                    }
                    str += string.Format(_template, InComeList[i], selected, VISS.Web.Frontend.Commons.IntToViewString(ConvertUtils.ToInt(InComeList[i])));
                }
            }

            return str;
        }
    }
}